<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use DB;
use Crypt;
class LoginController extends Controller
{
	//跳转到登录页面
	public function login()
	{
		return view('admin.login.login');
	}

	//用户登录
    public function dologin(Request $request)
    {
    	$data = $request -> except('_token');
    	

    	//检测验证码是否正确
    	$code = Session::get('milkcaptcha');
    	
    	if($data['captcha'] != $code)
    	{
    		return back() -> with(['info' => '验证码错误']);
    	}

    	//在数据库中查询提交的邮箱和密码
    	$res = DB::table('users') -> where('email',$data['email']) -> first();
    	if(!$res)
    	{
    		return back() -> with(['info' => '邮箱或密码错误']);
    	}
    	
    	$password = $res -> password;
    	//解密
    	$repassword = Crypt::decrypt($password);

    	if($data['password'] != $repassword)
    	{
    		return back() -> with(['info' => '邮箱或密码错误']);
    	}

    	if($res -> admin_user == 0)
    	{
    		return back() -> with(['info' => '您没有权限登录']);
    	}
    	session(['master_admin' => $res]);

    	return view('admin.index.index') -> with(['title' => '主页']);
    }
}
